﻿Imports MindFusion.Diagramming.Layout

Public Class ClsDiagramLayout
    'represents class for diagram layouting with various types of layouts

#Region "Attributes"
#Region "Private attributes"
    'all layout engines
    Private MoGridLayout As GridLayout = New GridLayout
    Private MoLayeredLayout As LayeredLayout = New LayeredLayout
    Private MoSpringLayout As SpringLayout = New SpringLayout
    Private MoTreeLayout As TreeLayout = New TreeLayout
    Private MoAnnealLayout As AnnealLayout = New AnnealLayout
    Private MoOrthogonalLayout As OrthogonalLayout = New OrthogonalLayout
    Private MoCircularLayout As CircularLayout = New CircularLayout
    Private MoOrthogonalRouter As OrthogonalRouter = New OrthogonalRouter
    Private MoFlowLayout As FlowLayout = New FlowLayout
    Private MoFlowChartLayout As FlowchartLayout = New FlowchartLayout
    Private MoTopologicalLayout As TopologicalLayout = New TopologicalLayout

    Private MoAllLayoutEngines As List(Of Layout) = New List(Of Layout)()
#End Region
#End Region
#Region "Events"
    Public Event MMAfterArranged(ByVal TypeOfLayout As GeTypeOfLayout) 'event is raised after arranging a diagram
#End Region
#Region "Constructors"
    Public Sub New()
        'for identifying desired engine in list is used enum GeTypeOfLayout
        MoAllLayoutEngines.Add(MoGridLayout)
        MoAllLayoutEngines.Add(MoLayeredLayout)
        MoAllLayoutEngines.Add(MoSpringLayout)
        MoAllLayoutEngines.Add(MoTreeLayout)
        MoAllLayoutEngines.Add(MoAnnealLayout)
        MoAllLayoutEngines.Add(MoOrthogonalLayout)
        MoAllLayoutEngines.Add(MoCircularLayout)
        MoAllLayoutEngines.Add(MoOrthogonalRouter)
        MoAllLayoutEngines.Add(MoFlowLayout)
        MoAllLayoutEngines.Add(MoFlowChartLayout)
        MoAllLayoutEngines.Add(MoTopologicalLayout)
    End Sub
#End Region
#Region "Subs"
    'arranges given diagram with desired layout engine
    Public Sub Arrange(ByVal IoDiagram As MindFusion.Diagramming.Diagram, ByVal IeTypeOfLayout As GeTypeOfLayout)
        Try
            'for identifying desired engine in list is used enum GeTypeOfLayout
            Dim LoLayout As Layout = MoAllLayoutEngines(IeTypeOfLayout)
            'preserving original links tyles of all links

            'backuping original link styles of all links
            Dim LoOriginalLinkStyles As Dictionary(Of ClsMMLinkNode, MindFusion.Diagramming.LinkStyle) = New Dictionary(Of ClsMMLinkNode, MindFusion.Diagramming.LinkStyle)
            For Each LoLinkNode As ClsMMLinkNode In IoDiagram.Links
                LoOriginalLinkStyles.Add(LoLinkNode, LoLinkNode.Style)
            Next
            LoLayout.Arrange(IoDiagram)
            'restoring original link styles of all links
            For Each LoLinkNode As ClsMMLinkNode In IoDiagram.Links
                LoLinkNode.Style = LoOriginalLinkStyles(LoLinkNode)
                LoLinkNode.SnapToNodeBorder = True
                LoLinkNode.AutoRoute = True
                LoLinkNode.AllowMoveStart = True
                LoLinkNode.AllowMoveEnd = True
                LoLinkNode.Dynamic = True
            Next

            'rerouting all links
            IoDiagram.RouteAllLinks()

            RaiseEvent MMAfterArranged(IeTypeOfLayout)
        Catch ex As Exception
            LogEventFromException(ex, , , )
        End Try
    End Sub

#End Region

End Class
